#!/bin/sh
#
# drivers_wupper:       Starts Wupper and cmem_rcc drivers on a WUPPER PC
#
# Version:      @(#) /etc/rc.d/init.d/drivers_wupper 2.0
#
# chkconfig: 2345 95 5
# description: Starts and stops tdaq drivers at boot time and shutdown.
#
# hide: true

export CMEM_PARAMS="gfpbpa_size=8192 gfpbpa_quantum=4"
#GFP based cmem_rcc:

# See how we were called.
case "$1" in
  start)
        echo "Starting cmem driver "
        # load the module
        modprobe cmem_rcc $CMEM_PARAMS
        # remove old device nodes
        rm -f /dev/cmem_rcc
        rm -f /dev/cmem_rcc_2
        # get major number
        major=`awk "\\$2==\"cmem_rcc\" {print \\$1}" /proc/devices`
        echo major number for cmem_rcc is $major
        # make device node
        mknod /dev/cmem_rcc c $major 0
        mknod /dev/cmem_rcc_2 c $major 1
        #give permissions
        chmod 666 /dev/cmem_rcc
        chmod 666 /dev/cmem_rcc_2
        echo

	export WUPPERCARDS=`/sbin/lspci -n | grep -c -e "10ee:703" -e "10dc:042"`
	echo $WUPPERCARDS wupper PCIe endpoints found
	FIRSTCARD=0
	if [ $WUPPERCARDS -gt 0 ]; then
	  echo "Starting wupper driver "
	  # load the module
          modprobe wupper
	  # remove old device nodes
	  rm -f /dev/wupper*
	  # get major number
	  major=`awk "\\$2==\"wupper\" {print \\$1}" /proc/devices`
	  echo major number for wupper is $major
	  for (( c=$FIRSTCARD; c<$WUPPERCARDS; c++ ))
	  do
	    echo "creating node /dev/wupper$c"
	    # make device node
	    mknod /dev/wupper$c c $major $c
	    # give permissions
	    chmod 666 /dev/wupper$c
	  done
          for filename in /dev/xil_xvc/cfg_ioc*; do
              echo changing mode of $filename to 666
              chmod 666 $filename
          done
	  #create a link from wupper0 to wupper for compatibility between new driver and old library
	  ln -s /dev/wupper0 /dev/wupper
	  echo
	fi
        ;;

  stop)
        echo "Shutting down cmem_rcc driver "
        /sbin/rmmod cmem_rcc
        # remove old device nodes
        rm -f /dev/cmem_rcc
        rm -f /dev/cmem_rcc_2

        echo "Shutting down io_rcc driver "
        /sbin/rmmod io_rcc
        # remove old device node
        rm -f /dev/io_rcc

        /sbin/lsmod | grep wupper
        if [ $? = 0 ]; then
          echo "Shutting down wupper driver "
          /sbin/rmmod wupper
	  # remove old device nodes
	  rm -f /dev/wupper*
        fi
        ;;

   status)
        /sbin/lsmod | grep cmem_rcc
        if [ $? = 0 ]; then
          echo ""
          echo ">>>>>> Status of the cmem_rcc driver"
          echo ""
          more /proc/cmem_rcc
        fi

        /sbin/lsmod | grep wupper
        if [ $? = 0 ]; then
          echo ""
          echo ">>>>>> Status of the wupper driver "
          echo ""
          more /proc/wupper
        fi
        ;;

  *)
        echo "*** Usage: drivers_wupper {start|stop|status}"
        exit 1

esac

exit 0
